package com.zhku.onlineshop.Service.impl;

import com.zhku.onlineshop.Dao.CartDao;
import com.zhku.onlineshop.Pojo.CartItem;
import com.zhku.onlineshop.Service.CartService;
import com.zhku.onlineshop.dto.CartItem_Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceimpl implements CartService {
    @Autowired
    private CartDao cartDao;
    @Override
    public boolean AddIntoCart(Integer user_id,Integer product_id) {
        //查看商品是否存在购物车中
        CartItem existingItem = cartDao.findByUserIdAndProductId(user_id, product_id);

    if(existingItem == null)
        return cartDao.AddIntoCart(user_id,product_id);
    else
        return cartDao.updateCart(user_id,product_id);
    }

    @Override
    public List<CartItem_Product> GetCartByID(Integer user_id) {
      return cartDao.GetCartByID(user_id);
    }

    @Override
    public boolean ReduceProductNum(Integer userId, Integer productId) {
        return cartDao.ReduceProductNum(userId,productId);
    }

    @Override
    public boolean DelProduct(Integer userId, Integer productId) {
        return cartDao.DelProduct(userId,productId);
    }
}
